#include "ContentType.h"

#include <boost/algorithm/algorithm.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <string>
namespace rdhttp {

    void ContentType::pares(std::string_view content) {
        data = content;

        if (data.empty())
            return;
        // multipart/form-data; boundary="boundary_.oOo._1/JYBIbzQIo4Q6HndVCcrQSStYAULUJ1"

        // multipart/form-data;boundary="boundary_.oOo._1/JYBIbzQIo4Q6HndVCcrQSStYAULUJ1"
        boost::erase_all(data, " ");

        auto value_end = data.find_first_of(';');
        m_value        = data.substr(0, value_end);
        boost::to_lower(m_value);

        auto boundary_start = data.find("boundary=");

        if (boundary_start == std::string::npos) return;

        boundary_start    = data.find_first_of('=', boundary_start) + 1;
        auto boundary_end = data.find_first_of(';', boundary_start + 1);

        m_boundary = data.substr(boundary_start, boundary_end - boundary_start);
        boost::erase_all(m_boundary, "\"");
    }

    ContentType::ContentType(std::string_view content) {
        pares(content);
    }

    ContentType::ContentType() {
    }

    ContentType::~ContentType() {
    }

    std::string ContentType::value() const {
        return std::string(m_value);
    }
    std::string ContentType::boundary() const {
        return std::string(m_boundary);
    }

    bool ContentType::operator==(ContentTypeValue v) const {
        switch (v) {
            case multipart_form_data:
                return m_value == "multipart/form-data";
            default:
                break;
        }
        return false;
    }
}  // namespace rdhttp
